<?php

defined('_SECURITY_TOKEN') or die("caia fora");

class controllerdownload //extends AppController
{

	function __construct(  )
	{
		
		$task	=	application::getTask();//( empty($_REQUEST['task'])? '':$_REQUEST['task']);
		
		switch( $task ){
			
			
			case 'accessMetadataNotation':
				
				
				
				try {	
        						
					//exige autenticacao de usuário
					//application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
								
					$result 	=	$this->getAccessKey($parameters->key, 2, $parameters->notation );
					
					$result=array("status"=>1,
									"data"=>application::arrayToJson($result),
									"notification"=>-1);
											
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    	
			    }
				
				break;
				
			case 'accessDownloadNotation':
				
				
				
				try {	
        						
					//exige autenticacao de usuário
					//application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
								
					$result 	=	$this->getAccessKey($parameters->key, 3, $parameters->notation );
					
					$result=array("status"=>1,
									"data"=>application::arrayToJson($result),
									"notification"=>-1);
											
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    	
			    }
				
				break;
				
			
			case 'accessDownloadAll':
				
				
				
				try {	
        						
					//exige autenticacao de usuário
					//application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
								
					$result 	=	$this->getAccessKey($parameters->key, 3, "" );
					
					$result=array("status"=>1,
									"data"=>application::arrayToJson($result),
									"notification"=>-1);
											
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    	
			    }
				
				break;
				
			case 'accessMetadataAll':
				
				
				
				try {	
        						
					//exige autenticacao de usuário
					//application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
								
					$result 	=	$this->getAccessKey($parameters->key, 2, "" );
					
					$result=array("status"=>1,
									"data"=>application::arrayToJson($result),
									"notification"=>-1);
											
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    	
			    }
				
				break;
				
				
			case 'access':
				
				
				
				try {	
        						
					//exige autenticacao de usuário
					//application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
								
					$result 	=	$this->getAccessKey($parameters->key, 1, "" );
					
					$result=array("status"=>1,
									"data"=>application::arrayToJson($result),
									"notification"=>-1);
											
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    	
			    }
				
				break;
			case 'accessAll':
				
				
				
				try {	
        						
					//exige autenticacao de usuário
					//application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();
								
					$result 	=	$this->getAccessKeyAllStatistics($parameters->key );
					
					$result=array("status"=>1,
									"data"=>application::arrayToJson($result),
									"notification"=>-1);
											
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    	
			    }
				
				break;	
			case 'getList':
				
				
				
				try {	
        						
					//exige autenticacao de usuário
					//application::IsAuthenticationRequired(true);
					
								
					$parameters =	application::getParameters();

					$result 	=	$this->getList(application::getUser_id(),
														$parameters->key,
														$parameters->limit,
														$parameters->offset );
					
					$result=array("status"=>1,
									"data"=>application::arrayToJson($result),
									"notification"=>-1);
											
	
					application::executeView( 'default', $result );
				
			    }
			    catch (AppException $e) {
			    	throw new AppException( $e->getMessage() );
			    	
			    }
				
				break;	
			default:
				
				//echo "view não definida.";
				throw new AppException( MSG_23_METODO_NAO_DEFINIDO );
				
				break;
		}
		
	}
	
	
	public function getList( $user_id, $key, $limit=10, $offset=0 ){
				
		$result=false;
		
		try{

			$model	=	application::getModel('statistics');
			
			$result	=	$model->getList($user_id, $key, $limit, $offset );
		
		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		

		return $result;
		
	}
	
	
	public function getAccessKeyAllStatistics( $key ){
				
		$result=false;
		
		try{

			$model	=	application::getModel('statistics');
			
			$result	=	$model->getAccessKeyAllStatistics( $key );
		
		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		

		return $result;
		
	}
	
	
	public function getAccessKey( $key , $metadata=1, $notation=""){
				
		$result=false;
		
		try{

			$model	=	application::getModel('statistics');
			
			if($metadata==1){
				$task_="all";
			}else if ($metadata==2){
				$task_="metadata";
			}else if ($metadata==3){
				$task_="download";
			}
			
			//exit("ccc".$metadata);
			$result	=	$model->getAccessKey( $key, $task_, $notation );
		
		}
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		

		return $result;
		
	}
	
	
}